package rahmath.sharif.docrypt;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import rahmath.sharif.docrypt.util.ExplorerListComparator;
import android.content.Context;
import android.content.res.Resources;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class ExplorerListAdapter extends ArrayAdapter<ExplorerListItem> {

	private List<ExplorerListItem> listItems = new ArrayList<ExplorerListItem>();
	private boolean chooseAny;

	public ExplorerListAdapter(Context context, int textViewResourceId,
			ArrayList<ExplorerListItem> listItems, boolean chooseAny) {
		super(context, textViewResourceId, listItems);
		Collections.sort(listItems, new ExplorerListComparator());
		this.listItems = listItems;
		this.chooseAny = chooseAny;
	}

	public View getView(final int position, final View convertView,
			final ViewGroup parent) {
		View view = convertView;
		if (view == null) {
			LayoutInflater vi = (LayoutInflater) getContext().getSystemService(
					Context.LAYOUT_INFLATER_SERVICE);
			view = vi.inflate(R.layout.list_item_file, null);
		}

		final ExplorerListItem explorerListItem = listItems.get(position);

		if (explorerListItem != null) {
			final TextView resName = (TextView) view
					.findViewById(R.id.explorerListItem);
			final Resources resources = getContext().getResources();
			if (resName != null) {
				resName.setText(explorerListItem.getFileName());
				resName.setCompoundDrawablesWithIntrinsicBounds(resources
						.getDrawable(explorerListItem.getDrawableInt()), null,
						null, null);
				boolean isFile = explorerListItem.isFile();
				if (isFile) {
					resName.setCompoundDrawablesWithIntrinsicBounds(
							resources.getDrawable(explorerListItem
									.getDrawableInt()),
							null,
							explorerListItem.isSelected() ? resources
									.getDrawable(R.drawable.btn_check_on_holo_dark)
									: resources
											.getDrawable(R.drawable.btn_check_off_holo_dark),
							null);
				} else if (chooseAny) {
					resName.setCompoundDrawablesWithIntrinsicBounds(
							resources.getDrawable(explorerListItem
									.getDrawableInt()),
							null,
							explorerListItem.isSelected() ? resources
									.getDrawable(R.drawable.btn_check_on_holo_dark)
									: resources
											.getDrawable(R.drawable.btn_check_off_holo_dark),
							null);
					((TextView) view.findViewById(R.id.explorerListItem))
							.setOnTouchListener(new View.OnTouchListener() {
								public boolean onTouch(View paramView,
										MotionEvent event) {
									if (resName.getCompoundDrawables()[2] == null) {
										return false;
									}
									if (event.getAction() != MotionEvent.ACTION_DOWN) {
										return false;
									}
									//decide whether or not to digin
									if (event.getX() > resName.getWidth()
											- resName.getPaddingRight()
											- resName.getCompoundDrawables()[2]
													.getIntrinsicWidth()) {
										explorerListItem.setDigin(false);
									} else {
										explorerListItem.setDigin(true);
									}
									return false;
								}
							});
				} else {//not a file and not chooseAny option so normal folder navigation
					explorerListItem.setDigin(true);
				}
			}
		}
		return view;
	}
}